﻿#ifndef COMMON_H
#define COMMON_H
#include <QObject>
#include <IMVAPI/IMVApi.h>
#include <stdexcept>
#include <array>
#include <QHash>
#define CaptureNum 9

typedef struct WoodProcessHistoryData
{
    int id;
    QString detect_time;        // 加工时间
    QString wood_type;          // 种类
    QString wood_processing;    // 加工方式
    double wood_ok_rate;        // 正常率
    double wood_length;         // 长度
    double wood_width;          // 宽度
    double wood_area;           // 面积
    double wood_dk;             // 死结率
    double wood_lk;             // 活结率
    double wood_ck;             // 裂纹
    double wood_pith;           // 髓芯
    WoodProcessHistoryData():
        id(-1),
        detect_time(""),
        wood_type(""),
        wood_processing(""),
        wood_ok_rate(0.0),
        wood_length(0.0),
        wood_width(0.0),
        wood_area(0.0),
        wood_dk(0.0),
        wood_lk(0.0),
        wood_ck(0.0),
        wood_pith(0.0){}
}WoodProcessHistoryData;

struct T_Batch {
    int id = 0;
    int user_id = 0;
    int plan_id = 0;
    int wood_num = 0;
    int wood_ng_num = 0;
    QString user_name;
    QString batch_name;
    QString plan_name;
    QString create_time;
    QString change_time;

    T_Batch() = default;
};

struct T_BatchLine {
    int id = 0;
    int batch_id = 0;
    QString wood_type;
    QString wood_processing;
    double wood_ok_rate = 0.0;
    double wood_length = 0.0;
    double wood_width = 0.0;
    double wood_line_area = 0.0;
    double wood_wood_area = 0.0;
    double wood_dk = 0.0;
    double wood_lk = 0.0;
    double wood_cr = 0.0;
    double wood_hb = 0.0;
    double wood_em = 0.0;
    double wood_wh = 0.0;
    double wood_sm = 0.0;
    double wood_bd = 0.0;
    double wood_pt = 0.0;
    double wood_rs = 0.0;
    QString cam_img_json;
    QString create_time;
    QString change_time;

    T_BatchLine() = default;
};

struct T_PlanLine {
    int id = 0;
    QString planName;
    int schemeId = 0;
    QString schemeName;
    QString scanName;
    QString woodType;
    QString createTime;
    QString changeTime;

    T_PlanLine() = default;
};

struct DefectSetting {
    int id = 0;
    int schemeId = 0;
    QString defectName;
    bool isActive = false;
    double longEdge = 0.0;
    double shortEdge = 0.0;
    double compensation = 0.0;
    bool openTogether = false;
    bool areaAssist = false;
    bool unacceptable = false;
    double edgeCorrection = 0.0;
    double confidence = 0.0;
    int sortOrder = 0;
    QString createTime;
    QString changeTime;

    DefectSetting() = default;
};

struct GeneralParam {
    int id = 0;
    int schemeId = 0;
    QString paramName;
    QString paramValue;
    int sortOrder = 0;
    QString createTime;
    QString changeTime;

    GeneralParam() = default;
};
struct QualityGrade {
    int id = 0;
    int schemeId = 0;
    QString grade;
    double weight = 0.0;
    int sortOrder = 0;
    QString createTime;
    QString changeTime;

    QualityGrade() = default;
};

struct EdgeParam {
    int id = 0;
    int schemeId = 0;
    QString paramName;
    QString paramValue;
    int sortOrder = 0;
    QString createTime;
    QString changeTime;

    EdgeParam() = default;
};

struct FixedLength {
    int id = 0;
    int schemeId = 0;
    QString fName;
    bool isActive = false;
    double length = 0.0;
    int sortOrder = 0;
    QString createTime;
    QString changeTime;

    FixedLength() = default;
};
struct DefectScheme {
    int schemeId = 0;
    QString schemeName;
    double headEnd = 0.0;
    double edgeSize = 0.0;
    bool isUnit = false;
    QString createTime;
    QString changeTime;
    QList<DefectSetting> defectSettings;
    QList<GeneralParam> generalParams;
    QList<QualityGrade> qualityGrades;
    QList<EdgeParam> edgeParams;
    QList<FixedLength> fixedLengths;

    DefectScheme() = default;
};

struct Detected_Data {
    double length = 0.0;
    int number = 0;
    int wood_ng = 0;
    double wood_speed = 0.0;
    double genuine_rate = 0.0;

    Detected_Data() = default;
};

// OK1: 代表“好的”或“无缺陷”
// LIGHTKNOT: 浅色木节（一种木材缺陷）。
// KNOT: 木节（比浅色木节更严重或更明显的木节）。
// PITH: 树心（指木材中心的髓心，有时视为缺陷）。
// CRACK: 裂纹。
// MINERAL: 矿物条纹（一种由矿物质沉积引起的缺陷）。
// SMALLKNOT: 小结节。
// BREAK: 断裂。
// ROUGH: 毛刺或粗糙表面

// struct GradeRuleConfig {
//     // 缺陷类型到严重等级的映射（key: 缺陷ID，value: 严重等级）
//     std::unordered_map<unsigned int, int> defectSeverityMap = {
//         {3, 3},  // PITH -> 关键缺陷
//         {4, 3},  // CRACK
//         {5, 3},  // MINERAL
//         {7, 3},  // BREAK
//         {2, 2},  // KNOT
//         {1, 1},  // LIGHTKNOT
//         {6, 1},  // SMALLKNOT
//         {8, 1}   // ROUGH
//     };

//     // 等级判定规则（可配置阈值）
//     struct {
//         int criticalFaces = 1;   // 关键缺陷面阈值（>=）
//         int majorFacesFor4 = 2;  // 触发4级的主要缺陷面数
//         int majorFacesFor3 = 1;  // 触发3级的主要缺陷面数
//         int minorFacesFor3 = 3;  // 触发3级的次要缺陷面数
//         int minorFacesFor2 = 1;  // 触发2级的次要缺陷面数
//     } thresholds;
// };

struct Thresholds {
    int criticalFaces = 1;   // 关键缺陷面数阈值
    int knotArea = 25;       // 结巴面积阈值（mm²）
    double unpolishedDepth = 1.5; // 未抛光深度阈值（mm）
    int mineralLineCount = 3; // 矿物线数量阈值
    int minCutLength = 250;   // 最短切割长度（mm）

    // 柱面料分级阈值
    int majorFacesFor4 = 2;   // 达到4级所需的主要缺陷面数
    int majorFacesFor3 = 1;   // 达到3级所需的主要缺陷面数
    int minorFacesFor3 = 3;   // 达到3级所需的次要缺陷面数
    int minorFacesFor2 = 1;   // 达到2级所需的次要缺陷面数
};

struct GradeRuleConfig {
    // 缺陷严重程度映射：缺陷ID -> 严重等级（0=none, 1=secondary, 2=main, 3=key）
    QHash<int, int> defectSeverityMap;

    Thresholds thresholds;    // 分级阈值
    bool isHandrail = false;  // 是否为扶手面料，默认为柱面料
};




struct MOTOR_CONTROL_INFO {
    int DI_Sensor1 = 4;
    int DO_Sensor1 = 4;
    int DI_Sensor2 = 5;
    int DO_Sensor2 = 5;
    int DI_Sensor3 = 7;
    int DO_Sensor3 = 7;
    int Start_Count_Sensor3 = 3500;
    int End_Count_Sensor3 = 7000;
    int DI_Sensor4 = 0;
    int DO_Sensor4 = 0;
    int Start_Count_Sensor4 = 0;
    int End_Count_Sensor4 = 0;
    int DI_MotorStart = 1;
    int DO_MotorStart = 0;
    int DI_MotorStop = 2;
    int DI_MotorEMOStop = 0;

    MOTOR_CONTROL_INFO() = default;
};


struct CameraSw {
    int m_nSource;
    int m_nComposite;
    int m_nOK;
    int m_nNG;
    CameraSw(int ng = 0, int ok = 0, int composite = 0, int source = 0)
        : m_nSource(source), m_nComposite(composite), m_nOK(ok), m_nNG(ng) {}
};

struct SaveSw
{
    CameraSw m_cSave1, m_cSave2, m_cSave3, m_cSave4, m_cSave5, m_cSave6;

    CameraSw& getCameraSw(int index) {
        switch(index) {
        case 1: return m_cSave1;
        case 2: return m_cSave2;
        case 3: return m_cSave3;
        case 4: return m_cSave4;
        case 5: return m_cSave5;
        case 6: return m_cSave6;
        default: throw std::out_of_range("Invalid group index");
        }
    }
};
// 成员指针类型别名
using CameraSwMember = int CameraSw::*;

inline constexpr std::array<CameraSwMember, 4> cameraSwMembers = {
    &CameraSw::m_nSource,
    &CameraSw::m_nComposite,
    &CameraSw::m_nOK,
    &CameraSw::m_nNG
};

// 帧信息
// frame imformation
class CFrameInfo
{
public:
    CFrameInfo()
    {
        m_pImageBuf = NULL;
        m_nBufferSize = 0;
        m_nWidth = 0;
        m_nHeight = 0;
        m_ePixelType = gvspPixelMono8;
        m_nPaddingX = 0;
        m_nPaddingY = 0;
        m_nTimeStamp = 0;
    }

    ~CFrameInfo()
    {
    }

public:
    QString cameraKey;
    std::shared_ptr<unsigned char> m_pImageBuf;
    int				m_nBufferSize;
    int				m_nWidth;
    int				m_nHeight;
    IMV_EPixelType	m_ePixelType;
    int				m_nPaddingX;
    int				m_nPaddingY;
    uint64_t		m_nTimeStamp;
};
#endif // COMMON_H
